package com.github_dididipapa.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github_dididipapa.mall.product.dao.BrandDao;
import com.github_dididipapa.mall.product.dao.CategoryDao;
import com.github_dididipapa.mall.product.dto.BrandRelationDto;
import com.github_dididipapa.mall.product.dto.CategoryDto;
import com.github_dididipapa.mall.product.entity.BrandEntity;
import com.github_dididipapa.mall.product.entity.CategoryEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github_dididipapa.mall.product.dao.CategoryBrandRelationDao;
import com.github_dididipapa.mall.product.entity.CategoryBrandRelationEntity;
import com.github_dididipapa.mall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    private final CategoryBrandRelationDao categoryBrandRelationDao;
    private final BrandDao brandDao;
    private final CategoryDao categoryDao;

    @Override
    public void save(Long brandId, Long catelogId, String brandName, String catelogName) {
        // 使用 QueryWrapper 构造安全查询
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId)
                    .eq("catelog_id", catelogId)
                    .eq("brand_name", brandName)
                    .eq("catelog_name", catelogName);

        // 执行查询判断是否存在记录
        if (this.count(queryWrapper) == 0) {
            this.save(brandId, catelogId, brandName, catelogName);
        }

    }

    //@Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询详细名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(
                categoryBrandRelationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>()
                        .eq("brand_id", brandId)
                );
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<BrandRelationDto> getBrandByCatId(Long catId) {
        List<BrandRelationDto> data = categoryBrandRelationDao.getBrandRelationByCatId(catId);

        return data;
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationDao.getCateGoryDtosByBrandId(brandId);
        return data;
    }
}